import { sys } from "cc";
import { HaoEncrypt } from "../engine/HaoEncrypt";
import HttpUtil from "../engine/HttpUtil";
import LocalStorage from "../engine/LocalStorage";
import { Md5Helper } from "../engine/Md5Helper";
import ServerTimer from "../engine/ServerTimer";
import UIManager from "../engine/UIManager";
import VersionManager from "../engine/VersionManager";
import { ErrorCode } from "../game/config/ErrorCode";
import { NativeSdkHelper } from "../sdk/NativeSdkHelper";
import { ServerConfig } from "./ServerConfig";
import { Logger } from "../engine/Logger";
import { AccountModel } from "../game/model/AccountModel";
import { SocketClient } from "./SocketClient";


export class HttpHelper {


    public static get_jhao(callback: Function) {
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getWebServerUrl() + "/login_jhao"
        let data: object = {
            "t": ServerTimer.now,
            "channel": NativeSdkHelper.channel,
            "v": VersionManager.instance.nowVersion
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            if (isOK) {
                if (response) {
                    let code: number = response["code"];
                    if (code == ErrorCode.Code.Suc) {
                        let data: string = response["data"]
                        let jhaoStr: string = HaoEncrypt.decode(data)
                        interface json {
                            anroid_url: string;
                            ios_url: string;
                            force_update: boolean;
                            need_hotupdate: boolean
                        }
                        let objVersion: json = JSON.parse(jhaoStr);
                        ServerConfig.initAccountServer(objVersion)
                        VersionManager.instance.apkStoreUrl = objVersion.anroid_url;
                        VersionManager.instance.iosStoreUrl = objVersion.ios_url;
                        let force_update: boolean = objVersion["force_update"]
                        let need_hotupdate: boolean = objVersion["need_hotupdate"];
                        if (force_update) {
                            VersionManager.instance.jumpToPackUpate()
                        } else {
                            callback(need_hotupdate)
                        }
                    } else {
                        callback(false)
                    }
                } else {

                }
            }
        }, JSON.stringify(param));
    }

    public static accountReg(account: string, password: string, callback: Function) {
        UIManager.instance.showLoadingIcon()
        LocalStorage.setItem("account", account)
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getAccountServerUrl() + "/register"
        let data: object = {
            "t": ServerTimer.now,
            "account": account,
            "password": password,
            "channel": NativeSdkHelper.channel,
            "os": sys.os,
            "phone_type": NativeSdkHelper.getPhoneType(),
            "bundle_name": NativeSdkHelper.getBundleName(),
            "system_version": NativeSdkHelper.getSystemVersion(),
            "udid": NativeSdkHelper.getUDID()
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            UIManager.instance.hideLoadingIcon()
            if (isOK) {
                Logger.log("accountReg==", response)
                if (response) {
                    let code: number = response["code"];
                    if (code == ErrorCode.Code.Suc) {
                        let data: object = response["data"];
                        AccountModel.instance.account = data["account"]
                        AccountModel.instance.token = data["token"]
                        ServerConfig.initHallServer(data)
                        callback && callback(true)
                        // SocketClient.login(true)
                    } else {
                        let msg: string = response["msg"]
                        // UIHelper.showTip(msg)
                        callback && callback(false)
                    }
                } else {

                }
            }
        }, JSON.stringify(param));
    }

    public static account_auth(account: string, password: string) {
        UIManager.instance.showLoadingIcon()
        LocalStorage.setItem("account", account)
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getAccountServerUrl() + "/account_auth"
        let data: object = {
            "t": ServerTimer.now,
            "account": account,
            "password": password,
            "channel": NativeSdkHelper.channel,
            "os": sys.os,
            "phone_type": NativeSdkHelper.getPhoneType(),
            "bundle_name": NativeSdkHelper.getBundleName(),
            "system_version": NativeSdkHelper.getSystemVersion(),
            "udid": NativeSdkHelper.getUDID()
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        Logger.log("account_auth=req=", data)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            UIManager.instance.hideLoadingIcon()
            if (isOK) {
                Logger.log("account_auth==", response)
                if (response) {
                    let code: number = response["code"];
                    if (code == ErrorCode.Code.Suc) {
                        let data: object = response["data"]
                        AccountModel.instance.account = data["account"]
                        AccountModel.instance.nickname = data["nickname"]
                        AccountModel.instance.password = password
                        AccountModel.instance.token = data["token"]
                        AccountModel.instance.save()
                        ServerConfig.initHallServer(data)
                        SocketClient.login(true)
                        // callback && callback(true)
                    } else {
                        let msg: string = response["msg"]
                        // UIHelper.showTip(msg)
                        // callback && callback(false)
                    }
                } else {

                }
            }
        }, JSON.stringify(param));
    }

    public static wechat_auth(code: string) {
        UIManager.instance.showLoadingIcon()
        let http: HttpUtil = new HttpUtil();
        let url: string = ServerConfig.getAccountServerUrl() + "/wechat_auth"
        let data: object = {
            "t": ServerTimer.now,
            "code": code,
            "channel": NativeSdkHelper.channel,
            "os": sys.os,
            "phone_type": NativeSdkHelper.getPhoneType(),
            "bundle_name": NativeSdkHelper.getBundleName(),
            "system_version": NativeSdkHelper.getSystemVersion(),
            "udid": NativeSdkHelper.getUDID()
        }
        let param: object = {
            "k": Md5Helper.getMd5_encrypt(JSON.stringify(data)),
            "data": data
        }
        http.setMethod(HttpUtil.methodTypeOption.POST)
        http.requestByBody(url, (isOK: boolean, response: any) => {
            UIManager.instance.hideLoadingIcon()
            if (isOK) {
                Logger.log("wechat_auth==", response)
                let code: number = response["code"];
                if (code == ErrorCode.Code.Suc) {
                    let data: object = response["data"];
                    AccountModel.instance.account = data["account"]
                    AccountModel.instance.token = data["token"]
                    AccountModel.instance.save()
                    ServerConfig.initHallServer(data)
                    // SocketClient.login(true)
                } else {
                    let msg: string = response["msg"]
                    // UIHelper.showTip(msg)
                }
            }
        }, JSON.stringify(param));
    }

   
  
    

    


  



}